package org.jeecg.modules.dmyweb.home.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.saobei.open.sdk.model.response.trade.SaobeiQrPayResponse;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.apache.shiro.SecurityUtils;
import org.jeecg.common.api.vo.Result;
import org.jeecg.common.aspect.annotation.AutoLog;
import org.jeecg.common.exception.JeecgBootException;
import org.jeecg.common.system.vo.LoginUser;
import org.jeecg.common.util.TokenUtils;
import org.jeecg.common.util.oConvertUtils;
import org.jeecg.modules.demo.advertisingsetting.entity.AdvertisingSetting;
import org.jeecg.modules.demo.advertisingsetting.service.IAdvertisingSettingService;
import org.jeecg.modules.demo.baseorde.service.IBaseOrderService;
import org.jeecg.modules.demo.commoditymanagement.service.ICommodityManagementService;
import org.jeecg.modules.demo.inventorybase.service.IInventoryBaseService;
import org.jeecg.modules.demo.membermanagement.entity.MemberManagement;
import org.jeecg.modules.demo.membermanagement.service.IMemberManagementService;
import org.jeecg.modules.demo.rechargecardcost.entity.RechargeCardCost;
import org.jeecg.modules.demo.rechargecardcost.service.IRechargeCardCostService;
import org.jeecg.modules.demo.rechargerecord.entity.RechargeRecord;
import org.jeecg.modules.demo.rechargerecord.service.IRechargeRecordService;
import org.jeecg.modules.demo.saobeiorderrecord.entity.SaobeiOrderRecord;
import org.jeecg.modules.demo.saobeiorderrecord.service.ISaobeiOrderRecordService;
import org.jeecg.modules.demo.stockmanage.entity.StockManage;
import org.jeecg.modules.demo.stockmanage.service.IStockManageService;
import org.jeecg.modules.demo.storeinfo.entity.StoreInfo;
import org.jeecg.modules.demo.storeinfo.service.IStoreInfoService;
import org.jeecg.modules.dmyweb.base.service.IBaseService;
import org.jeecg.modules.demo.widthmanagement.service.IWidthManagementService;
import org.jeecg.modules.demo.allvipcard.service.IAllVipcardService;
import org.jeecg.modules.dmyweb.home.service.IHomeService;
import org.jeecg.modules.saobei.Saobei;
import org.jeecg.modules.system.entity.SysRole;
import org.jeecg.modules.system.service.ISysRoleService;
import org.jeecg.modules.util.QRCodeUtil;
import org.jeecg.modules.util.RedisUtils;
import org.jeecg.modules.util.ToolsUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.transaction.interceptor.TransactionAspectSupport;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.math.BigDecimal;
import java.text.SimpleDateFormat;
import java.util.*;

@Api(tags="dmyweb-首页")
@RestController
@RequestMapping("/dmyweb/home")
@Slf4j
public class HomeController {
    @Autowired
    private IStoreInfoService storeInfoService;
    @Autowired
    private IRechargeCardCostService rechargeCardCostService;
    @Autowired
    private IRechargeRecordService rechargeRecordService;
    @Autowired
    private IMemberManagementService memberManagementService;
    @Autowired
    private IStockManageService stockManageService;
    @Autowired
    private IBaseOrderService baseOrderService;
    @Autowired
    private ICommodityManagementService commodityManagementService;
    @Autowired
    private IInventoryBaseService inventoryBaseService;
    @Autowired
    private IWidthManagementService  widthManagementService;
    @Autowired
    private IAllVipcardService allVipcardService;
    @Autowired
    private IAdvertisingSettingService advertisingSettingService;
    @Autowired
    private ISysRoleService sysRoleService;
    @Autowired
    private IHomeService homeService;
    @Autowired
    private ISaobeiOrderRecordService saobeiOrderRecordService;

    @Autowired
    private RedisUtils redisUtils;

    /**
     * 获取门店
     * storeExpiryDate店铺有效期
     * remainDays 剩余天数
     */
    @AutoLog(value = "首页-获取门店")
    @ApiOperation(value = "首页-获取门店", notes = "首页-获取门店")
    @GetMapping(value = "/getStoreInfo")
    public Result<?> getStoreInfo(){

        LoginUser sysUser = (LoginUser) SecurityUtils.getSubject().getPrincipal();
        String roleType = sysUser.getRoleType();
        if ("管理员".equals(roleType)) {
            return Result.OK(storeInfoService.list());
        } else if ("老板".equals(roleType)) {
            return Result.OK(storeInfoService.list(new QueryWrapper<StoreInfo>().eq("boss_id", sysUser.getId())));
        } else if ("员工".equals(roleType)) {
            return Result.OK(storeInfoService.list(new QueryWrapper<StoreInfo>().eq("id", sysUser.getStoreId())));
        }
        // 角色类型为“其他”
        return Result.OK(storeInfoService.list());
    }

    /**
     * 更新成功店铺信息
     */
    @AutoLog(value = "店铺信息-更新成功店铺信息")
    @ApiOperation(value="店铺信息-更新成功店铺信息", notes="店铺信息-更新成功店铺信息")
    @PutMapping(value = "/updataUserStoreId")
    @Transactional(rollbackFor = JeecgBootException.class)
    public Result<?> updataUserStoreId(@RequestBody StoreInfo storeInfo, HttpServletRequest request) {

        String token = TokenUtils.getTokenByRequest(request);
        // storeInfo只传了1个storeId
        StoreInfo si = storeInfoService.getById(storeInfo.getId());
        Date storeExpiryDate = si.getStoreExpiryDate();
        if (storeExpiryDate.compareTo(new Date()) < 0) {
            redisUtils.set(RedisUtils.storeId + token, storeInfo.getId() + ",过期",12L);
            return Result.OK("店铺已过期，仅能进行续费操作");
        }
        redisUtils.set(RedisUtils.storeId + token,storeInfo.getId(),12L);
        return Result.OK("门店切换成功");
    }

    //获取店铺ID
    @GetMapping("/getStoreId")
    public Result<?> getStoreId(HttpServletRequest request) {
        String token = TokenUtils.getTokenByRequest(request);
        String storeId = (String) redisUtils.get(RedisUtils.storeId +token);
        return Result.OK(storeId);
    }

    //店铺缓存 清空缓存
    @GetMapping(value = "/cleanRedis")
    private void cleanRedis(){
        redisUtils.cleanRedis(RedisUtils.storeId);
    }

    /**
     * 充值卡选择
     */
    @AutoLog(value = "首页-充值卡选择")
    @ApiOperation(value = "首页-充值卡选择", notes = "首页-充值卡选择")
    @GetMapping(value = "/getRechargecard")
    public Result<?> getRechargecard(){

        RechargeCardCost rechargeCardCost = rechargeCardCostService.getById('1');
        if(rechargeCardCost == null){
            return Result.error("需要相关人员先设置充值卡费用");
        }
        List<String>list = new LinkedList<>();
        if (rechargeCardCost.getYearCost() != null) {
            list.add("年卡");
        }
        if (rechargeCardCost.getHalfyearCost() != null) {
            list.add("半年卡");
        }
        if (rechargeCardCost.getQuarterCost() != null) {
            list.add("季度卡");
        }
        if (rechargeCardCost.getMonthCost() != null) {
            list.add("月卡");
        }
        if (list.size() == 0) {
            return Result.error("需要相关人员先设置充值卡费用");
        }
        return Result.OK(list);
    }

    /**
     * 续费充值
     */
    @AutoLog(value = "首页-续费充值")
    @ApiOperation(value = "首页-续费充值", notes = "首页-续费充值")
    @PutMapping(value = "/renewal")
    @Transactional(rollbackFor = JeecgBootException.class)
    public Result<?> renewal(@RequestBody Map<String, Object> map, HttpServletRequest req, HttpServletResponse res){
        String token = TokenUtils.getTokenByRequest(req);
        String storeId = (String) redisUtils.get(RedisUtils.storeId +token);
        if(oConvertUtils.isEmpty(storeId)){
            return Result.error("请先到首页选择店铺");
        }

        LoginUser sysUser = (LoginUser) SecurityUtils.getSubject().getPrincipal();

        RechargeRecord rechargeRecord = new RechargeRecord();

        RechargeCardCost rechargeCardCost = rechargeCardCostService.getById('1');
        if(rechargeCardCost == null){
            return Result.error("需要相关人员先设置充值卡费用");
        }
        String rechargeType = map.get("rechargeType") == null ? null : String.valueOf(map.get("rechargeType"));
        if (oConvertUtils.isEmpty(rechargeType)) {
            return Result.error("缺少rechargeType参数");
        }
        if(rechargeType.equals("年卡")){
            Double yearCost = rechargeCardCost.getYearCost();
            if (yearCost == null) {
                return Result.error("需要相关人员先设置年卡费用");
            }
            rechargeRecord.setRechargeAmount(yearCost);
        }
        if(rechargeType.equals("半年卡")){
            Double halfyearCost = rechargeCardCost.getHalfyearCost();
            if (halfyearCost == null) {
                return Result.error("需要相关人员先设置半年卡费用");
            }
            rechargeRecord.setRechargeAmount(halfyearCost);
        }
        if(rechargeType.equals("季度卡")){
            Double quarterCost = rechargeCardCost.getQuarterCost();
            if (quarterCost == null) {
                return Result.error("需要相关人员先设置季度卡费用");
            }
            rechargeRecord.setRechargeAmount(quarterCost);
        }
        if(rechargeType.equals("月卡")){
            Double monthCost = rechargeCardCost.getMonthCost();
            if (monthCost == null) {
                return Result.error("需要相关人员先设置月卡费用");
            }
            rechargeRecord.setRechargeAmount(monthCost);
        }

        rechargeRecord.setOrderNumber(ToolsUtils.getBillNo("XF"));
        rechargeRecord.setRechargeType(rechargeType);
        rechargeRecord.setOperator(sysUser.getRealname());
        rechargeRecord.setStoreId(storeId);
        rechargeRecord.setOrderStatus("0");
        rechargeRecordService.save(rechargeRecord);

        BigDecimal rechargeAmount = new BigDecimal(String.valueOf(rechargeRecord.getRechargeAmount()));
        String totalFee = String.valueOf(rechargeAmount.multiply(new BigDecimal("100")).intValue());

        SaobeiQrPayResponse qrPayResponse = Saobei.qrpay(Saobei.ACCESS_TOKEN, Saobei.MERCHANT_NO, Saobei.TERMINAL_ID, "店铺续费", rechargeRecord.getId(), totalFee);
        if ("01".equals(qrPayResponse.getResult_code())) {
            SaobeiOrderRecord saobeiOrderRecord = new SaobeiOrderRecord();
            saobeiOrderRecord.setType("1");
            saobeiOrderRecord.setTerminalTrace(rechargeRecord.getId());
            saobeiOrderRecord.setTotalFee(totalFee);
            saobeiOrderRecord.setStoreId(storeId);
            saobeiOrderRecord.setCreateBy("xcx");
            saobeiOrderRecordService.save(saobeiOrderRecord);

            String qrUrl = qrPayResponse.getQr_url();
            try {
                QRCodeUtil.createCodeToOutputStream(qrUrl, res.getOutputStream());
            } catch (IOException e) {
                TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
                return Result.error("生成二维码失败：" + qrPayResponse.getReturn_msg());
            }
            return null;
        } else {
            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
            return Result.error("生成扫呗订单失败：" + qrPayResponse.getReturn_msg());
        }
    }

    /**
     * 订单数量 暂无，等收银台功能
     * today month
     */
    @AutoLog(value = "首页-订单数量")
    @ApiOperation(value = "首页-订单数量", notes = "首页-订单数量")
    @GetMapping(value = "/getOrderNum")
    public Result<?> getOrderNum(HttpServletRequest req){
        String token = TokenUtils.getTokenByRequest(req);
        String storeId = (String) redisUtils.get(RedisUtils.storeId +token);
        if(oConvertUtils.isEmpty(storeId)){
            Map<String,Object>map = new HashMap<>();
            map.put("today",0);
            map.put("month",0);
            return Result.OK(map);
        }
        Map<String,Object>map = new HashMap<>();
        String todayQty = baseOrderService.getOrderQtyToday(storeId);
        String thisMonthQty = baseOrderService.getOrderQtyThisMonth(storeId);
        map.put("today",todayQty);
        map.put("month",thisMonthQty);
        return Result.OK(map);
    }

    /**
     * 销售额
     */
    @AutoLog(value = "首页-销售额")
    @ApiOperation(value = "首页-销售额", notes = "首页-销售额")
    @GetMapping(value = "/getSales")
    public Result<?> getSales(HttpServletRequest req){
        //HttpServletRequest req
        String token = TokenUtils.getTokenByRequest(req);
        String storeId = (String) redisUtils.get(RedisUtils.storeId +token);
        if(oConvertUtils.isEmpty(storeId)){
            Map<String,Object>map = new HashMap<>();
            map.put("today",0);
            map.put("month",0);
            return Result.OK(map);
        }
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        String formatToDay = sdf.format(new Date());
        SimpleDateFormat sdf2 = new SimpleDateFormat("yyyy-MM");
        String formatMonth = sdf2.format(new Date());

        Map<String,Object> dataToDay = homeService.getDataFormat(storeId,formatToDay,"");
        Map<String,Object> dataMonth = homeService.getDataFormat(storeId,"",formatMonth);
        Map<String,Object>map = new HashMap<>();
        map.put("today",dataToDay.get("saleroom"));
        map.put("month",dataMonth.get("saleroom"));
        return Result.OK(map);
    }

    /**
     * 店铺会员数
     */
    @AutoLog(value = "首页-店铺会员数")
    @ApiOperation(value = "首页-店铺会员数", notes = "首页-店铺会员数")
    @GetMapping(value = "/getStoreMemberNum")
    public Result<?> getStoreMemberNum(HttpServletRequest req){
        //HttpServletRequest req
        String token = TokenUtils.getTokenByRequest(req);
        String storeId = (String) redisUtils.get(RedisUtils.storeId +token);
        if(oConvertUtils.isEmpty(storeId)){
            Map<String,Object>map = new HashMap<>();
            map.put("today",0);
            map.put("month",0);
            return Result.OK(map);
        }

        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        String formatToDay = sdf.format(new Date());
        SimpleDateFormat sdf2 = new SimpleDateFormat("yyyy-MM");
        String formatMonth = sdf2.format(new Date());

        QueryWrapper<MemberManagement> queryToDay = new QueryWrapper<>();
        queryToDay.eq("store_id",storeId);
        queryToDay.apply("DATE_FORMAT(create_time,'%Y-%m-%d') = '"+formatToDay+"'");
        int today = memberManagementService.count(queryToDay);

        QueryWrapper<MemberManagement> queryMonth = new QueryWrapper<>();
        queryMonth.eq("store_id",storeId);
        queryMonth.apply("DATE_FORMAT(create_time,'%Y-%m') = '"+formatMonth+"'");
        int month = memberManagementService.count(queryMonth);

        Map<String,Object>map = new HashMap<>();
        map.put("today",today);
        map.put("month",month);
        return Result.OK(map);
    }

    /**
     * 员工商品提成
     */
    @AutoLog(value = "首页-员工商品提成")
    @ApiOperation(value = "首页-员工商品提成", notes = "首页-员工商品提成")
    @GetMapping(value = "/getStaffShopAward")
    public Result<?> getStaffShopAward(HttpServletRequest req){
        //HttpServletRequest req
        String token = TokenUtils.getTokenByRequest(req);
        String storeId = (String) redisUtils.get(RedisUtils.storeId +token);
        if(oConvertUtils.isEmpty(storeId)){
            Map<String,Object>map = new HashMap<>();
            map.put("today",0);
            map.put("month",0);
            return Result.OK(map);
        }
        Map<String,Object>map = new HashMap<>();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        String formatToDay = sdf.format(new Date());
        SimpleDateFormat sdf2 = new SimpleDateFormat("yyyy-MM");
        String formatMonth = sdf2.format(new Date());

        Map<String,Object> dataToDay = homeService.getDataFormat(storeId,formatToDay,"");
        Map<String,Object> dataMonth = homeService.getDataFormat(storeId,"",formatMonth);

        map.put("today",dataToDay.get("staffShopAward"));
        map.put("month",dataMonth.get("staffShopAward"));
        return Result.OK(map);
    }

    /**
     * 员工项目提成
     */
    @AutoLog(value = "首页-员工项目提成")
    @ApiOperation(value = "首页-员工项目提成", notes = "首页-员工项目提成")
    @GetMapping(value = "/getStaffJobAward")
    public Result<?> getStaffJobAward(HttpServletRequest req){
        //HttpServletRequest req
        String token = TokenUtils.getTokenByRequest(req);
        String storeId = (String) redisUtils.get(RedisUtils.storeId +token);
        if(oConvertUtils.isEmpty(storeId)){
            Map<String,Object>map = new HashMap<>();
            map.put("today",0);
            map.put("month",0);
            return Result.OK(map);
        }
        Map<String,Object>map = new HashMap<>();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        String formatToDay = sdf.format(new Date());
        SimpleDateFormat sdf2 = new SimpleDateFormat("yyyy-MM");
        String formatMonth = sdf2.format(new Date());

        Map<String,Object> dataToDay = homeService.getDataFormat(storeId,formatToDay,"");
        Map<String,Object> dataMonth = homeService.getDataFormat(storeId,"",formatMonth);

        map.put("today",dataToDay.get("staffProjectAward"));
        map.put("month",dataMonth.get("staffProjectAward"));
        return Result.OK(map);
    }

    /**
     * 股东数量
     */
    @AutoLog(value = "首页-股东数量")
    @ApiOperation(value = "首页-股东数量", notes = "首页-股东数量")
    @GetMapping(value = "/getStockNum")
    public Result<?> getStockNum(HttpServletRequest req){
        String token = TokenUtils.getTokenByRequest(req);
        String storeId = (String) redisUtils.get(RedisUtils.storeId +token);
        if(oConvertUtils.isEmpty(storeId)){
            Map<String,Object>map = new HashMap<>();
            map.put("today",0);
            map.put("month",0);
            return Result.OK(map);
        }

        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        String formatToDay = sdf.format(new Date());
        SimpleDateFormat sdf2 = new SimpleDateFormat("yyyy-MM");
        String formatMonth = sdf2.format(new Date());

        QueryWrapper<StockManage> queryToDay = new QueryWrapper<>();
        queryToDay.eq("store_id",storeId);
        queryToDay.apply("DATE_FORMAT(create_time,'%Y-%m-%d') = '"+formatToDay+"'");
        int today = stockManageService.count(queryToDay);

        QueryWrapper<StockManage> queryMonth = new QueryWrapper<>();
        queryMonth.eq("store_id",storeId);
        queryMonth.apply("DATE_FORMAT(create_time,'%Y-%m') = '"+formatMonth+"'");
        int month = stockManageService.count(queryMonth);

        Map<String,Object>map = new HashMap<>();
        map.put("today",today);
        map.put("month",month);
        return Result.OK(map);
    }

    /**
     * 带动营业额
     */
    @AutoLog(value = "首页-带动营业额")
    @ApiOperation(value = "首页-带动营业额", notes = "首页-带动营业额")
    @GetMapping(value = "/getStockYield")
    public Result<?> getStockYield(HttpServletRequest req){
        String token = TokenUtils.getTokenByRequest(req);
        String storeId = (String) redisUtils.get(RedisUtils.storeId +token);
        if(oConvertUtils.isEmpty(storeId)){
            Map<String,Object>map = new HashMap<>();
            map.put("today",0);
            map.put("month",0);
            return Result.OK(map);
        }
        Map<String,Object>map = new HashMap<>();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        String formatToDay = sdf.format(new Date());
        SimpleDateFormat sdf2 = new SimpleDateFormat("yyyy-MM");
        String formatMonth = sdf2.format(new Date());

        Map<String,Object> dataToDay = homeService.getDataFormat(storeId,formatToDay,"");
        Map<String,Object> dataMonth = homeService.getDataFormat(storeId,"",formatMonth);

        map.put("today",dataToDay.get("profit"));
        map.put("month",dataMonth.get("profit"));
        return Result.OK(map);
    }

    /**
     * 股东会员数 和股东重复了
     */
    @AutoLog(value = "首页-股东会员数")
    @ApiOperation(value = "首页-股东会员数", notes = "首页-股东会员数")
    @GetMapping(value = "/getStockMemberNum")
    public Result<?> getStockMemberNum(HttpServletRequest req){
        //
        String token = TokenUtils.getTokenByRequest(req);
        String storeId = (String) redisUtils.get(RedisUtils.storeId +token);
        if(oConvertUtils.isEmpty(storeId)){
            Map<String,Object>map = new HashMap<>();
            map.put("today",0);
            map.put("month",0);
            return Result.OK(map);
        }

        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        String formatToDay = sdf.format(new Date());
        SimpleDateFormat sdf2 = new SimpleDateFormat("yyyy-MM");
        String formatMonth = sdf2.format(new Date());

        QueryWrapper<MemberManagement> queryToDay = new QueryWrapper<>();
        queryToDay.eq("store_id",storeId);
        queryToDay.eq("member_type","股东会员");
        queryToDay.apply("DATE_FORMAT(create_time,'%Y-%m-%d') = '"+formatToDay+"'");
        int today = memberManagementService.count(queryToDay);

        QueryWrapper<MemberManagement> queryMonth = new QueryWrapper<>();
        queryMonth.eq("store_id",storeId);
        queryToDay.eq("member_type","股东会员");
        queryMonth.apply("DATE_FORMAT(create_time,'%Y-%m') = '"+formatMonth+"'");
        int month = memberManagementService.count(queryMonth);

        Map<String,Object>map = new HashMap<>();
        map.put("today",today);
        map.put("month",month);
        return Result.OK(map);
    }

    /**
     * 股东分红
     */
    @AutoLog(value = "首页-股东分红")
    @ApiOperation(value = "首页-股东分红", notes = "首页-股东分红")
    @GetMapping(value = "/getStockAward")
    public Result<?> getStockAward(HttpServletRequest req){
        String token = TokenUtils.getTokenByRequest(req);
        String storeId = (String) redisUtils.get(RedisUtils.storeId +token);
        if(oConvertUtils.isEmpty(storeId)){
            Map<String,Object>map = new HashMap<>();
            map.put("today",0);
            map.put("month",0);
            return Result.OK(map);
        }
        Map<String,Object>map = new HashMap<>();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        String formatToDay = sdf.format(new Date());
        SimpleDateFormat sdf2 = new SimpleDateFormat("yyyy-MM");
        String formatMonth = sdf2.format(new Date());

        Map<String,Object> dataToDay = homeService.getDataFormat(storeId,formatToDay,"");
        Map<String,Object> dataMonth = homeService.getDataFormat(storeId,"",formatMonth);

        map.put("today",dataToDay.get("stockBonus"));
        map.put("month",dataMonth.get("stockBonus"));
        return Result.OK(map);
    }

    /**
     * 股东管家分红
     */
    @AutoLog(value = "首页-股东管家分红")
    @ApiOperation(value = "首页-股东管家分红", notes = "首页-股东管家分红")
    @GetMapping(value = "/getStockStewardAward")
    public Result<?> getStockStewardAward(HttpServletRequest req){
        String token = TokenUtils.getTokenByRequest(req);
        String storeId = (String) redisUtils.get(RedisUtils.storeId +token);
        if(oConvertUtils.isEmpty(storeId)){
            Map<String,Object>map = new HashMap<>();
            map.put("today",0);
            map.put("month",0);
            return Result.OK(map);
        }
        Map<String,Object>map = new HashMap<>();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        String formatToDay = sdf.format(new Date());
        SimpleDateFormat sdf2 = new SimpleDateFormat("yyyy-MM");
        String formatMonth = sdf2.format(new Date());

        Map<String,Object> dataToDay = homeService.getDataFormat(storeId,formatToDay,"");
        Map<String,Object> dataMonth = homeService.getDataFormat(storeId,"",formatMonth);

        map.put("today",dataToDay.get("staffBonus"));
        map.put("month",dataMonth.get("staffBonus"));
        return Result.OK(map);
    }

    /**
     * 店铺数据  暂无，等收银台功能
     * stayPayOrder  待付款订单
     * refundOrder   退款中订单
     * offerShop     出售中商品
     * urgencyShop   库存告急商品
     * withdrawalCheck  提现待审核
     * sendVip        已下发贵宾卡
     */
    @AutoLog(value = "首页-店铺数据")
    @ApiOperation(value = "首页-店铺数据", notes = "首页-店铺数据")
    @GetMapping(value = "/getStoreData")
    public Result<?> getStoreData(){
        Map<String,Object>map = new HashMap<>();
        map.put("stayPayOrder",0);
        map.put("refundOrder",0);
        map.put("offerShop",0);
        map.put("urgencyShop",0);
        map.put("withdrawalCheck",0);
        map.put("sendVip",0);
        return Result.OK();
    }

    /**
     * 广告列表
     */
    @AutoLog(value = "首页-广告列表")
    @ApiOperation(value = "首页-广告列表", notes = "首页-广告列表")
    @GetMapping(value = "/getAdList")
    public Result<?> getAdList(HttpServletRequest req){
        String token = TokenUtils.getTokenByRequest(req);
        String storeId = (String) redisUtils.get(RedisUtils.storeId +token);
        if(oConvertUtils.isEmpty(storeId)){
            Map<String,Object>map = new HashMap<>();
            map.put("today",0);
            map.put("month",0);
            return Result.OK(map);
        }

        QueryWrapper<AdvertisingSetting> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("store_id", storeId);
        List<AdvertisingSetting> pageList = advertisingSettingService.list(queryWrapper);
        return Result.OK(pageList);
    }

    /**
     * 近7天营业额走势
     * dataX
     * data
     */
    @AutoLog(value = "首页-近7天营业额走势")
    @ApiOperation(value = "首页-近7天营业额走势", notes = "首页-近7天营业额走势")
    @GetMapping(value = "/getTurnover")
    public Result<?> getTurnover(HttpServletRequest req){
        String token = TokenUtils.getTokenByRequest(req);
        String storeId = (String) redisUtils.get(RedisUtils.storeId +token);
        if(oConvertUtils.isEmpty(storeId)){
            return Result.OK("");
        }
        Map<String,Object>map = new HashMap<>();
        List<String> dataX = new ArrayList<>();
        List<Object> data = new ArrayList<>();
        for(int i=0;i<7;i++){
            String day = ToolsUtils.getLastDay(-(7-i));
            Map<String,Object> dataToDay = homeService.getDataFormat(storeId,day,"");
            dataX.add(day);
            data.add(dataToDay.get("saleroom"));
        }
        map.put("dataX",dataX);
        map.put("data",data);
        return Result.OK(map);
    }

    /**
     * 近7天客流量走势
     * dataX
     * data
     */
    @AutoLog(value = "首页-近7天客流量走势")
    @ApiOperation(value = "首页-近7天客流量走势", notes = "首页-近7天客流量走势")
    @GetMapping(value = "/getPassenger")
    public Result<?> getPassenger(HttpServletRequest req){
        String token = TokenUtils.getTokenByRequest(req);
        String storeId = (String) redisUtils.get(RedisUtils.storeId +token);
        if(oConvertUtils.isEmpty(storeId)){
            return Result.OK("");
        }
        Map<String,Object>map = new HashMap<>();
        List<String> dataX = new ArrayList<>();
        List<String> data = new ArrayList<>();
        for(int i=0;i<7;i++){
            String day = ToolsUtils.getLastDay(-(7-i));
            String todayQty = baseOrderService.getOrderQtyday(storeId,day);
            dataX.add(day);
            data.add(todayQty);
        }
        map.put("dataX",dataX);
        map.put("data",data);
        return Result.OK(map);
    }

    /**
     * 查询待付款订单数量
     */
    @AutoLog(value = "首页-查询待付款订单数量")
    @ApiOperation(value = "首页-查询待付款订单数量", notes = "首页-查询待付款订单数量")
    @GetMapping(value = "/getPendingPayOrderQty")
    public String getPendingPayOrderQty(HttpServletRequest req){
        String token = TokenUtils.getTokenByRequest(req);
        String storeId = (String) redisUtils.get(RedisUtils.storeId +token);
        if(oConvertUtils.isEmpty(storeId)){
            return "0";
        }
        return baseOrderService.getPendingPayOrderQty(storeId);
    }

    /**
     * 查询退款中订单数量
     */
    @AutoLog(value = "首页-查询退款中订单数量")
    @ApiOperation(value = "首页-查询退款中订单数量", notes = "首页-查询退款中订单数量")
    @GetMapping(value = "/getRefundingOrderQty")
    public String getRefundingOrderQty(HttpServletRequest req){
        String token = TokenUtils.getTokenByRequest(req);
        String storeId = (String) redisUtils.get(RedisUtils.storeId +token);
        if(oConvertUtils.isEmpty(storeId)){
            return "0";
        }
        return baseOrderService.getRefundingOrderQty(storeId);
    }

    /**
     * 查询出售中商品数量
     */
    @AutoLog(value = "首页-查询出售中商品数量")
    @ApiOperation(value = "首页-查询出售中商品数量", notes = "首页-查询出售中商品数量")
    @GetMapping(value = "/getOnSaleCommodityQty")
    public String getOnSaleCommodityQty(HttpServletRequest req){
        String token = TokenUtils.getTokenByRequest(req);
        String storeId = (String) redisUtils.get(RedisUtils.storeId +token);
        if(oConvertUtils.isEmpty(storeId)){
            return "0";
        }
        return commodityManagementService.getOnSaleCommodityQty(storeId);
    }

    /**
     * 查询库存告急商品
     */
    @AutoLog(value = "首页-查询库存告急商品")
    @ApiOperation(value = "首页-查询库存告急商品", notes = "首页-查询库存告急商品")
    @GetMapping(value = "/getInventoryUrgentQty")
    public String getInventoryUrgentQty(HttpServletRequest req){
        String token = TokenUtils.getTokenByRequest(req);
        String storeId = (String) redisUtils.get(RedisUtils.storeId +token);
        if(oConvertUtils.isEmpty(storeId)){
            return "0";
        }
        return inventoryBaseService.getInventoryUrgentQty(storeId);
    }

    /**
     * 查询提现待审核数量
     */
    @AutoLog(value = "首页-查询提现待审核数量")
    @ApiOperation(value = "首页-查询提现待审核数量", notes = "首页-查询提现待审核数量")
    @GetMapping(value = "/getWithdrawalToReview")
    public String getWithdrawalToReview(HttpServletRequest req){
        String token = TokenUtils.getTokenByRequest(req);
        String storeId = (String) redisUtils.get(RedisUtils.storeId +token);
        if(oConvertUtils.isEmpty(storeId)){
            return "0";
        }
        return widthManagementService.getWithdrawalToReview(storeId);
    }

    /**
     * 查询已下发贵宾卡数量
     */
    @AutoLog(value = "首页-查询已下发贵宾卡数量")
    @ApiOperation(value = "首页-查询已下发贵宾卡数量", notes = "首页-查询已下发贵宾卡数量")
    @GetMapping(value = "/getGrantedCardQty")
    public String getGrantedCardQty(HttpServletRequest req){
        String token = TokenUtils.getTokenByRequest(req);
        String storeId = (String) redisUtils.get(RedisUtils.storeId +token);
        if(oConvertUtils.isEmpty(storeId)){
            return "0";
        }
        return allVipcardService.getGrantedCardQty(storeId);
    }


}
